// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
(function (win: Window, lib: any) {
  const doc: Document = win.document;
  const docEl: HTMLElement = doc.documentElement;
  let metaEl: HTMLMetaElement | null = doc.querySelector(
    'meta[name="viewport"]',
  );
  const flexibleEl: HTMLMetaElement | null = doc.querySelector(
    'meta[name="flexible"]',
  );
  let dpr = 0;
  let scale = 0;
  let tid: number | undefined;
  const flexible: any = lib.flexible || (lib.flexible = {});

  if (metaEl) {
    console.warn('将根据已有的meta标签来设置缩放比例');
    const match: RegExpMatchArray | null = metaEl
      .getAttribute('content')!
      .match(/initial-scale=([\d.]+)/);
    if (match) {
      scale = parseFloat(match[1]);
      dpr = parseInt((1 / scale).toString(), 10);
    }
  } else if (flexibleEl) {
    const content: string | null = flexibleEl.getAttribute('content');
    if (content) {
      const initialDpr: RegExpMatchArray | null =
        content.match(/initial-dpr=([\d.]+)/);
      const maximumDpr: RegExpMatchArray | null =
        content.match(/maximum-dpr=([\d.]+)/);
      if (initialDpr) {
        dpr = parseFloat(initialDpr[1]);
        scale = parseFloat((1 / dpr).toFixed(2));
      }
      if (maximumDpr) {
        dpr = parseFloat(maximumDpr[1]);
        scale = parseFloat((1 / dpr).toFixed(2));
      }
    }
  }

  if (!dpr && !scale) {
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    const isAndroid: RegExpMatchArray | null =
      win.navigator.appVersion.match(/android/gi);
    const isIPhone: RegExpMatchArray | null =
      win.navigator.appVersion.match(/iphone/gi);
    const devicePixelRatio: number = win.devicePixelRatio;
    if (isIPhone) {
      if (devicePixelRatio >= 3 && (!dpr || dpr >= 3)) {
        dpr = 3;
      } else if (devicePixelRatio >= 2 && (!dpr || dpr >= 2)) {
        dpr = 2;
      } else {
        dpr = 1;
      }
    } else {
      dpr = 1;
    }
    scale = 1 / dpr;
  }

  docEl.setAttribute('data-dpr', dpr.toString());

  if (!metaEl) {
    metaEl = doc.createElement('meta');
    metaEl.setAttribute('name', 'viewport');
    metaEl.setAttribute(
      'content',
      'initial-scale=' +
        scale +
        ', maximum-scale=' +
        scale +
        ', minimum-scale=' +
        scale +
        ', user-scalable=no',
    );
    if (docEl.firstElementChild) {
      docEl.firstElementChild.appendChild(metaEl);
    } else {
      const wrap: HTMLDivElement = doc.createElement('div');
      wrap.appendChild(metaEl);
      doc.write(wrap.innerHTML);
    }
  }

  function refreshRem() {
    let width: number = docEl.getBoundingClientRect().width;
    if (width / dpr > 540) {
      width = 540 * dpr;
    }
    const rem: number = width / 10;
    docEl.style.fontSize = rem + 'px';
    flexible.rem = rem;
    win['rem'] = rem;
  }

  win.addEventListener(
    'resize',
    function () {
      if (tid) {
        clearTimeout(tid);
      }
      tid = setTimeout(refreshRem, 300);
    },
    false,
  );

  win.addEventListener(
    'pageshow',
    function (e: PageTransitionEvent) {
      if (e.persisted) {
        if (tid) {
          clearTimeout(tid);
        }
        tid = setTimeout(refreshRem, 300);
      }
    },
    false,
  );

  if (doc.readyState === 'complete') {
    doc.body.style.fontSize = (12 * dpr).toString() + 'px';
  } else {
    doc.addEventListener(
      'DOMContentLoaded',
      function () {
        doc.body.style.fontSize = (12 * dpr).toString() + 'px';
      },
      false,
    );
  }

  refreshRem();
  flexible.dpr = dpr;
  flexible.refreshRem = refreshRem;

  flexible.rem2px = function (d: string | number): string | number {
    let val: string | number = parseFloat(d.toString()) * this.rem;
    if (typeof d === 'string' && d.match(/rem$/)) {
      val += 'px';
    }
    return val;
  };

  flexible.px2rem = function (d: string | number): string | number {
    let val: string | number = parseFloat(d.toString()) / this.rem;
    if (typeof d === 'string' && d.match(/px$/)) {
      val += 'rem';
    }
    return val;
  };
})(window, window['lib'] || (window['lib'] = {}));
